package main

import (
	"fmt"
	"reflect"
)

type student struct {
	Name   string
	Number int
	Height uint
	Weight float64
}

func (s student) Info() {
	fmt.Printf("Name:\t%s\n", s.Name)
	fmt.Printf("Number:\t%03d\n", s.Number)
	fmt.Printf("Height:\t%d cm\n", s.Height)
	fmt.Printf("Weight:\t%.2f kg\n", s.Weight)
}

func (s *student) SetHeightWeight(h uint, w float64) {
	s.Height = h
	s.Weight = w
}

func main() {
	s1 := student{"Jimmy", 1, 170, 65.5}
	// 遍历字段属性
	st1 := reflect.TypeOf(s1)
	for i := 0; i < st1.NumMethod(); i++ {
		fmt.Println("方法名：", st1.Method(i).Name, st1.Method(i).Type)
	}
	// 遍历字段的值
	sv1 := reflect.ValueOf(s1)
	for i := 0; i < sv1.NumMethod(); i++ {
		fmt.Println("方法值: ", sv1.Method(i))
	}

	s2 := student{"Mary", 2, 160, 45.5}
	st2 := reflect.TypeOf(&s2)
	sv2 := reflect.ValueOf(&s2) // 这里传了指针
	// 遍历字段的属性和值
	for i := 0; i < st2.NumMethod(); i++ {
		fmt.Printf("%v(%v): %v\n", st2.Method(i).Name, st2.Method(i).Type, sv2.Method(i))
	}
}
/* 输出结果：
方法属性： Info func(main.student)
方法值:  0x5cff60
Info(func(*main.student)): 0x5cff60
SetHeightWeight(func(*main.student, uint, float64)): 0x5cff60
*/